package ${package.ServiceImpl};

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaya.common.param.RequestParam;
import com.yaya.common.result.ResponseResult;
import ${config.packageConfig.parent}.domain.${table.entityName};
import ${config.packageConfig.parent}.dtomain.${table.entityName}Dto;
import ${config.packageConfig.parent}.mapper.${table.mapperName};

/**
 * ${table.comment}服务实现
 *
 * @author ${author}
 * @since ${date[0..10]}
 */
@Service
@Transactional
public class ${table.serviceImplName} implements ${table.serviceName} {

  @Autowired
  private ${table.mapperName} ${table.mapperName?uncap_first};

  /**
   * 新增${table.comment}
   *
   * @param ${table.entityName?uncap_first}Dto ${table.comment}传输对象
   * @return 新增条数
   */
  @Override
  public int insert(${table.entityName}Dto ${table.entityName?uncap_first}Dto) {
    ${table.entityName} ${table.entityName?uncap_first} = ${table.entityName}.build${table.entityName}(${table.entityName?uncap_first}Dto);
    ${table.entityName?uncap_first}.setInsertUser("1");
    return ${table.mapperName?uncap_first}.insert(${table.entityName?uncap_first});
  }

  /**
   * 批量新增${table.comment}
   *
   * @param ${table.entityName?uncap_first}Dtos ${table.comment}传输对象列表
   * @return 新增条数
   */
  @Override
  public int inserts(List<${table.entityName}Dto> ${table.entityName?uncap_first}Dtos) {
    List<${table.entityName}> ${table.entityName?uncap_first}s = ${table.entityName}.build${table.entityName}s(${table.entityName?uncap_first}Dtos);
    for (${table.entityName} ${table.entityName?uncap_first} : ${table.entityName?uncap_first}s) {
      ${table.entityName?uncap_first}.setInsertUser("1");
    }
    return ${table.mapperName?uncap_first}.inserts(${table.entityName?uncap_first}s);
  }

  /**
   * 删除${table.comment}
   *
   * @param ${table.entityName?uncap_first}Ids ${table.comment}主键列表
   * @return 删除条数
   */
  @Override
  public int delete(List<String> ${table.entityName?uncap_first}Ids) {
    return ${table.mapperName?uncap_first}.deleteBatchIds(${table.entityName?uncap_first}Ids);
  }

  /**
   * 修改${table.comment}
   *
   * @param ${table.entityName?uncap_first}Dto ${table.comment}传输对象
   * @return 修改条数
   */
  @Override
  public int update(${table.entityName}Dto ${table.entityName?uncap_first}Dto) {
    ${table.entityName} ${table.entityName?uncap_first} = ${table.entityName}.build${table.entityName}(${table.entityName?uncap_first}Dto);
    return ${table.mapperName?uncap_first}.updateById(${table.entityName?uncap_first});
  }

  /**
   * 查询所有${table.comment}
   *
   * @param ${table.entityName?uncap_first}Dto ${table.comment}查询对象
   * @return ${table.comment}传输对象列表
   */
  @Override
  public List<${table.entityName}Dto> list(${table.entityName}Dto ${table.entityName?uncap_first}Dto) {
    QueryWrapper<${table.entityName}> wrapper = new QueryWrapper<${table.entityName}>();
<#list table.fields as field>
<#if !field.keyFlag>
    wrapper.eq(ObjectUtils.isNotEmpty(${table.entityName?uncap_first}Dto.get${field.propertyName?cap_first}()), "${field.columnName}",
        ${table.entityName?uncap_first}Dto.get${field.propertyName?cap_first}());
</#if>
</#list>
    List<${table.entityName}> ${table.entityName?uncap_first}s = ${table.mapperName?uncap_first}.selectList(wrapper);
    return ${table.entityName}.build${table.entityName}Dtos(${table.entityName?uncap_first}s);
  }

  /**
   * 分页查询${table.comment}
   *
   * @${table.entityName?uncap_first}Dto 基本参数
   * @${table.entityName?uncap_first}Dto ${table.comment}查询对象
   * @return 分页信息
   */
  @Override
  public Map<String, Object> page(RequestParam<${table.entityName}Dto> ${table.entityName?uncap_first}Dto) {
    Page<${table.entityName}> page = ${table.entityName?uncap_first}Dto.buildPage();
    QueryWrapper<${table.entityName}> wrapper = new QueryWrapper<${table.entityName}>();
<#list table.fields as field>
<#if !field.keyFlag>
    wrapper.eq(ObjectUtils.isNotEmpty(param.getData().get${field.propertyName?cap_first}()), "${field.columnName}",
        param.getData().get${field.propertyName?cap_first}());
</#if>
</#list>
    page = ${table.mapperName?uncap_first}.selectPage(page, wrapper);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(ResponseResult.TOTAL_TAG, page.getTotal());
    map.put(ResponseResult.RECORDS_TAG, page.getRecords());
    return map;
  }

}
